<?php

namespace app\common\model\Business;

use think\Model;

// 引入软删除
use traits\model\SoftDelete;

/**
 * 客户模型
 */
class Business extends Model
{
    // 引用软删除
    use SoftDelete;

    // 表名
    protected $name = 'business';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名 在插入语句的时候 会自动写入时间戳
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 忽略数据表不存在的字段
    protected $field = true;

    // 追加自定义字段属性 别名
    protected $append = [
        'avatar_text',
        'mobile_text',
        'gender_text',
        'deal_text',
        // 地区获取器
        'province_text',
        'city_text',
        'district_text',
    ];

    // 给追加的新字段赋值
    public function getAvatarTextAttr($value, $data)
    {
        $avatar = isset($data['avatar']) ? $data['avatar'] : '';

        // 路径判断 要用相对路径  ./
        if(!is_file(".".$avatar))
        {
            // 给个默认图
            $avatar = '/assets/home/images/avatar.jpg';
        }

        // 获取系统配置里面的选项
        $url = config('site.url') ? config('site.url') : '';

        // 拼上域名信息
        $avatar = trim($avatar, '/');
        $avatar = $url.'/'.$avatar;
        
        return $avatar;
    }

    public function getMobileTextAttr($value, $data)
    {
        $mobile = isset($data['mobile']) ? trim($data['mobile']) : '';

        // 将手机号码中间4位换成*
        return substr_replace($mobile, '****',3,4);
    }

    public function getGenderList()
    {
        return ['0' => __('保密'), '1' => __('男'), '2' => __('女')];
    }

    public function getDealList()
    {
        return ['0' => __('未成交'), '1' => __('已成交')];
    }


    public function getGenderTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
        $list = $this->getGenderList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDealTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['deal']) ? $data['deal'] : '');
        $list = $this->getDealList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function source()
    {
        return $this->belongsTo('app\common\model\Business\Source', 'sourceid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function visit()
    {
        return $this->belongsTo('app\common\model\Business\Visit', 'id', 'busid', [], 'LEFT')->setEagerlyType(0);
    }

    public function admin()
    {
        return $this->belongsTo('app\common\model\Business\Admin', 'adminid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getProvinceTextAttr($value, $data)
    {
        $province = $data['province'];

        if(empty($province))
        {
            return '';
        }

        return model('Region')->where(['code' => $province])->value('name');
    }

    public function getCityTextAttr($value, $data)
    {
        $city = $data['city'];

        if(empty($city))
        {
            return '';
        }

        return model('Region')->where(['code' => $city])->value('name');
    }

    public function getDistrictTextAttr($value, $data)
    {
        $district = $data['district'];

        if(empty($district))
        {
            return '';
        }

        return model('Region')->where(['code' => $district])->value('name');
    }
}
